package mk.gamemap.view
{
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	import game.tileMap.model.vo.Character;
	import game.tileMap.model.vo.MapObj;
	import mk.gamemap.model.Player;
	import mk.gamemap.model.Tile;
	import mk.gamemap.model.TileMapModel0;
	import mk.gamemap.view.sprite.CharView;
	import mk.gamemap.view.sprite.SpriteView;
	import game.tileMap.SpriteCache;
	import game.tileMap.view.PlayerView;

	public class TileMapView
	{
		//public var mapModel:TileMapModel
		public var mapCanvas:Sprite
		public var spCanvas:Sprite
		public var canvas:Sprite
		public var tileSize:int=SpriteCache.TileSize;
		public var player:CharView
		public var mapSprites:Array
		private var spFactory:SpriteCache;
		
		
		
		private var mapModel:TileMapModel0
		
		var scroll_rec:Rectangle
		var maxRec:Rectangle
		public function TileMapView(mapModel:TileMapModel0)
		{
			this.mapModel=mapModel;
			spFactory=new SpriteCache();
			mapCanvas=new Sprite();
			spCanvas=new Sprite();
			canvas=new Sprite();
			canvas.addChild(mapCanvas);
			canvas.addChild(spCanvas);
			mapSprites=[];
			
			
			scroll_rec=new Rectangle(0,0,800,600);//显示的范围
			canvas.scrollRect=scroll_rec;//确定地图的可显示范围
			maxRec=canvas.getBounds(canvas);//以自己的坐标系返回矩形
		}
		
		public function drawMap():void{
			dispose();
			for(var i:int=0;i<mapModel.tiles.length;i++){
				for(var j:int=0;j<mapModel.tiles[0].length;j++){
					var tile:Tile=mapModel.tiles[i][j]
					var tileView:Sprite=spFactory.drawTile(tile.id)
					tileView.x=i*tileSize;
					tileView.y=j*tileSize;
					mapCanvas.addChild(tileView);
				}
				
			}
			
			for each(var mapSp:MapObj in mapModel.mapSprites){
				
				
				if (mapSp is Player){
					if (!player){
						player=new PlayerView( Character(mapSp ));
					}
					
					spCanvas.addChild(player.sp);
					trace("加入主角"+mapSp)
				}else	if (mapSp is Character){
					var char:CharView=new CharView( Character(mapSp ));
					//player.sp=spFactory.producePlayer()
					spCanvas.addChild(char.sp);
					
				}else{
					var sp:SpriteView=new SpriteView(mapSp);
					
					spCanvas.addChild(sp.sp);
					sp.update();
				}
				mapSprites.push(sp);
				
			}
			
		}
		
		
		private function clearSp(sp:Sprite):void{
			while(sp.numChildren>0){
				sp.removeChildAt(0);
			}
		}
		public function dispose():void{
			clearSp(spCanvas);
			clearSp(mapCanvas);
			mapSprites=[];
		}
		
		public function scroll_h(distance:int):void{
			scroll_rec.x-=distance;
			canvas.scrollRect=scroll_rec;
		}
		public function scroll_v(distance:int):void{
			scroll_rec.y-=distance;
			canvas.scrollRect=scroll_rec;
		}
		
		public function addPlayer(playerView:Sprite):void{
			
			mapCanvas.addChild(playerView);
		}
		
		public function update():void{
			for each(var spView:SpriteView in mapSprites){
				if (spView){
					spView.update();
				}
				
			}
			//scroll_h(-1)
			last_x=player.sp.x
			last_y = player.sp.y
			player.update();
			//update_scroll(last_x, last_y)
			
		}
		
		private var last_x:int,last_y:int;
		public var CENTER_Y:int=100;
		public var CENTER_X:int=100;
		public function update_scroll(last_real_x, last_real_y):void{
			var ax1:int =last_x
			var ay1:int = last_y
			var ax2:int =player.sp.x
			var ay2:int = player.sp.y
			
				var scx:int=ax2-scroll_rec.x
				var scy:int=ay2-	scroll_rec.y
			//		trace(scx+":::"+scy)
			if (ax2 < ax1 &&scx<CENTER_X){
				scroll_h(ax1 - ax2)
			}
			if (ax2 > ax1&&scx>CENTER_X ){
				scroll_h(ax1 - ax2)
			}
			
			if (ay2 > ay1 &&scy > CENTER_Y){
				scroll_v(ay1 - ay2)
			}
			if (ay2 < ay1 &&scy < CENTER_Y){
				scroll_v(ay1 - ay2)
			}
			
		}
		
		
	}
}